package com.wfcm.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;

import com.wfcm.annotation.IgnoreSign;
import com.wfcm.entity.*;
import com.wfcm.service.*;
import com.wfcm.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wfcm.annotation.IgnoreToken;

/**
 * 
 * @content 课程章节Controller
 */
@RequestMapping("/course")
@Controller
public class CourseController {
	private Logger log = LoggerFactory.getLogger(AdInfoController.class);
	@Autowired
	WfCourseChapterService courseChapterService;
	@Autowired
	WfCourseInfoService courseInfoService;
	@Autowired
	private WfVipCardService vipCardService;
	@Autowired
	private WfCourseCommentService courseCommentService;
	@Autowired
	private WfChapterPlayRecordService chapterPlayRecordService;
	@Autowired
	private WfChapterRecordService chapterRecordService;
	@Autowired
	private WfGoodsOrderService goodsOrderService;
	@Autowired
	private WfGoodsOrderRelationService goodsOrderRelationService ;	
	@Autowired
	private WfCollectionService collectionService ;
	@Autowired
	private WfCommenmanagerService commenmanagerService ;
	@Autowired
	private WfCourseDownloadLogsService ourseDownloadLogsService;
	@Autowired
	private WfCardCourseRelationService cardCourseRelationService;
	@Autowired
	private WfCollageActivityService collageActivityService;
	@Autowired
	private DistributionService distributionService;
	@Autowired
	private WfStudyTimeService studyTimeService;
	@Autowired
	private RecordService recordService;
	@Autowired
	private WfCourseWorkService courseWorkService;
	@Autowired
	private WfMemberService memberService;

	/**
	 * 评论分页查询接口
	 */
	@RequestMapping("/pageComment")
	@ResponseBody
	@IgnoreToken
	public R pageComment(HttpServletRequest req) {

		Integer userId = ISsoLoginHelper.confirm(req);
		int id = Integer.valueOf(req.getParameter("chapterId"));
		int page = Integer.valueOf(req.getParameter("page"));
		log.debug("chapterId:"+id);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("chapterId", id);
		map.put("page", page);
		if (userId != null) map.put("userId", userId);
		//查询列表数据
        Query query = new Query(map);
        //评论总数
        map.put("commentType", 2);
		int commentTotal = courseCommentService.queryTotal(map);
		//评论列表
		List<WfCourseCommentEntity> courseCommentList = courseCommentService.queryCommentList(query);
		PageUtils pageUtil = new PageUtils(courseCommentList, commentTotal, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 课程分类列表
	 */
	@RequestMapping("/courseInfoList")
	@ResponseBody
	@IgnoreToken
	public R courseInfoList(HttpServletRequest req) {
		
		String courseCategoryId = req.getParameter("courseCategoryId");
		String ageId = req.getParameter("ageId");
		log.debug("courseCategoryId:"+courseCategoryId);
		log.debug("ageId:"+ageId);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("courseCategoryId",courseCategoryId);
		map.put("ageId", ageId);
		List<WfCourseInfoEntity> courseInfoList = courseInfoService.queryCategoryList(map);
		//查询相关课程包
		List<Map<String,Object>> vipCardTeacher =  new ArrayList<Map<String,Object>>();
		List<WfVipCardEntity> vipCardlist =  new ArrayList<WfVipCardEntity>();
		vipCardlist = vipCardService.queryCategoryList(map);
		for(int k=0; k<vipCardlist.size(); k++) {
			int vcId = vipCardlist.get(k).getId();
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("vipCardId", vcId);
			List<WfVipCardEntity> vipCardList = vipCardService.queryCourseList(map1);
			Map<String,Object> map2 = new HashMap<String,Object>();
			map2.put("vipCardTeacher", vipCardList);
			map2.put("vipCard", vipCardlist.get(k));
			vipCardTeacher.add(map2);
		}
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("courseInfolist", courseInfoList);
		resultMap.put("vipCardlist", vipCardTeacher);
		
		return R.ok(resultMap);
	}
	
	/**
	 * 章节播放记录
	 */
	@RequestMapping("/savePlayLog")
	@ResponseBody
	public R savePlayLog(HttpServletRequest req) {
		
		//获取章节id
		String chapterId = req.getParameter("chapterId");
		Integer uid = ISsoLoginHelper.confirm(req);//获取userid
		log.debug("chapterId:"+chapterId);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("chapterId", chapterId);
		map.put("uid", uid);
		
		//查询播放次数
		WfChapterRecordEntity chapterRecord = chapterRecordService.queryObject(Integer.valueOf(chapterId));
		if(null != chapterRecord) {
			int playTotal = chapterRecord.getNumber() + 1;
			chapterRecord.setNumber(playTotal);
			chapterRecordService.update(chapterRecord);
		}else {
			chapterRecord = new WfChapterRecordEntity();
			chapterRecord.setChapterid(Integer.valueOf(chapterId));
			chapterRecord.setNumber(1);
			chapterRecord.setAddtime(new Date());
			chapterRecord.setUpdatetime(new Date());
			chapterRecordService.save(chapterRecord);
		}
		
		WfChapterPlayRecordEntity chapterPlayRecordEntity = new WfChapterPlayRecordEntity();
		chapterPlayRecordEntity.setChapterid(Integer.valueOf(chapterId));
		chapterPlayRecordEntity.setUserid(uid);
		chapterPlayRecordEntity.setPlaytime(new Date());
		chapterPlayRecordService.save(chapterPlayRecordEntity);
		
		return R.ok();
	}
	
	/**
	 * 播放浮窗
	 */
	@RequestMapping("/playLog")
	@ResponseBody
	public R playLogList(HttpServletRequest req) {
		R r = R.ok();
		Integer uid = ISsoLoginHelper.confirm(req);//获取userid
		WfChapterPlayRecordEntity chapterPlayRecord= chapterPlayRecordService.queryPlaylog(uid);
		if(chapterPlayRecord == null){
            return r;
        }
		WfCourseChapterEntity courseChapter = courseChapterService.queryChapter(chapterPlayRecord.getChapterid());

		r.put("chapterName", courseChapter.getChaptername());
		r.put("courseName", courseChapter.getWfCourseInfoEntity().getCoursename());
		r.put("speakerImage", courseChapter.getWfSpeakerEntity().getSpeakerimage());
		r.put("chapterId", courseChapter.getId());
		return r;
	}

	/**
	 * 播放记录列表
	 */
	@RequestMapping("/playLogList")
	@ResponseBody
	public R playLog(HttpServletRequest req) {
		Integer uid = ISsoLoginHelper.confirm(req);//获取userid

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("uid",uid);
		List<WfChapterPlayRecordEntity> chapterPlayRecordList = chapterPlayRecordService.queryList(map);
		return R.ok().put("chapterPlayRecordList", chapterPlayRecordList);
	}

	/**
	 * 播放记录列表,分页查询
	 */
	@RequestMapping("/playLogListPage")
	@ResponseBody
	public R playLogListPage(HttpServletRequest req) {
		Integer uid = ISsoLoginHelper.confirm(req);//获取userid
		int page = Integer.valueOf(req.getParameter("page"));

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("uid",uid);
		List<WfChapterPlayRecordEntity> chapterPlayRecordList = chapterPlayRecordService.queryList(map);
		//查询列表数据
		map.put("page",page);
		Query query = new Query(map);
		//播放记录列表
		List<WfChapterPlayRecordEntity> chapterPlayRecordListPage = chapterPlayRecordService.queryList(query);
		PageUtils pageUtil = new PageUtils(chapterPlayRecordListPage, chapterPlayRecordList.size(), query.getLimit(), query.getPage());

		return R.ok().put("chapterPlayRecordList", pageUtil);
	}

	/**
	 * 删除播放记录（可多选）
	 */
	@RequestMapping("/delPlayLog")
	@ResponseBody
	public R delPlayLog(HttpServletRequest req) {
		String log = req.getParameter("log");

		String[] logArr = log.split(",");
		List<String> logList1 = Arrays.asList(logArr);//数组转为list
		List<String> logList = new ArrayList(logList1);
		for (int i = 0; i <logList.size() ; i++) {
			chapterPlayRecordService.delete(Integer.valueOf(logList.get(i)));
		}
		return R.ok();
	}

	/**
	 * （new）课程信息接口
	 */
	@ResponseBody
	@RequestMapping("/courseInfo")
	@IgnoreToken
	public R newCourseInfo(HttpServletRequest req, Integer courseId) {

		Integer userId = ISsoLoginHelper.confirm(req);
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> collMap = new HashMap<>();
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("access", -1);
		if(null != userId) {
			cardCourseRelationService.queryCardIdByCourseId(courseId).stream().forEach(item -> {
				if (goodsOrderRelationService.queryOrderCode(
						new WfGoodsOrderRelationEntity(userId, item, 3)).size() > 0) {
					resultMap.put("access", 1);
				}
			});
			if (resultMap.get("access").equals(-1) && goodsOrderRelationService.queryOrderCode(
					new WfGoodsOrderRelationEntity(userId, courseId, 1)).size() > 0) {
				resultMap.put("access", 1);
			}
		}

		Map courseMap = new HashMap();
		courseMap.put("type",1);
		courseMap.put("foreignid",courseId);
		List<WfCollageActivityEntity> collageList = collageActivityService.queryByCourseId(courseMap);
		if(collageList.size() > 0){
			resultMap.put("collageFlag", 1);
		}else {
			resultMap.put("collageFlag", 0);
		}

		map.put("courseId", courseId);
		map.put("notput", 1);//是否限时下架（非null即显示）
		//查询是否收藏
		collMap.put("type", 1);//1.课程 2.章节3.vip卡4商品
		collMap.put("userId", userId);
		collMap.put("foreignId", courseId);
		resultMap.put("chapterlist", courseChapterService.queryList(map));
		WfCourseInfoEntity courseInfoEntity = courseInfoService.queryObject(courseId);
		courseInfoEntity.setLabel(courseInfoService.queryCourseLabel(1, courseInfoEntity.getId()));
		resultMap.put("courseInfoEntity", courseInfoEntity);
		resultMap.put("collection", collectionService.queryCollectionList(collMap).size() > 0 ? 1 : 0);
		Distribution d1 = distributionService.queryDistributionByKey("h5_notice");
		Distribution d2 = distributionService.queryDistributionByKey("ios_notice");
		Distribution d3 = distributionService.queryDistributionByKey("android_notice");
		resultMap.put("h5_notice", d1 != null ? d1.getRemark() : "");
		resultMap.put("ios_notice", d2 != null ? d2.getRemark() : "");
		resultMap.put("android_notice", d3 != null ? d3.getRemark() : "");
		resultMap.put("distributionStatus", distributionService.searchGoods(courseId, 1) ? 1 : 2);
		return R.ok(resultMap);
	}

	/**
	 * （new）章节信息接口
	 */
	@RequestMapping("/courseChapter")
	@ResponseBody
	@IgnoreToken
	public R newCourseChapter(HttpServletRequest req, Integer chapterId) {
		Integer userId = ISsoLoginHelper.confirm(req);
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object> greatMap = new HashMap<>();
		Map<String, Object> collMap = new HashMap<>();
		Map<String, Object> map = new HashMap<>();

		resultMap.put("access", -1);
		resultMap.put("courseFlag", -1);
		WfCourseChapterEntity courseChapter = courseChapterService.queryChapter(chapterId);
		if(null != userId) {
			cardCourseRelationService.queryCardIdByCourseId(courseChapter.getCourseid()).stream().forEach(item -> {
				if (goodsOrderRelationService.queryOrderCode(
						new WfGoodsOrderRelationEntity(userId, item, 3)).size() > 0) {
					resultMap.put("access", 1);
					resultMap.put("courseFlag", 1);
				}
			});
			if (resultMap.get("access").equals(-1) && goodsOrderRelationService.queryOrderCode(
					new WfGoodsOrderRelationEntity(userId, courseChapter.getCourseid(), 1)).size() > 0) {
				resultMap.put("access", 1);
				resultMap.put("courseFlag", 1);
			}
			if (resultMap.get("access").equals(-1) && courseChapter.getWfCourseInfoEntity().getCoursetype() == 1) {
				resultMap.put("courseFlag", 1);
			}
			if (resultMap.get("access").equals(-1) && goodsOrderRelationService.queryOrderCode(
					new WfGoodsOrderRelationEntity(userId, chapterId, 2)).size() > 0) {
				resultMap.put("access", 1);
			}
		}
		//查询 1.点赞 2.评论
		map.put("chapterId", chapterId);
		//查询是否点赞
		greatMap.put("chapterid", chapterId);
		greatMap.put("userid", userId);
		greatMap.put("commenttype", 1);
		//查询是否收藏
		collMap.put("foreignId", chapterId);
		collMap.put("userId", userId);
		//1.课程 2.章节 3.vip卡 4.商品
		collMap.put("type", 2);
		courseChapter.setLabel(courseInfoService.queryCourseLabel(2, courseChapter.getId()));
		resultMap.put("courseChapterEntity", courseChapter);
		map.put("commentType", 1);
		resultMap.put("greatTotal", courseCommentService.queryTotal(map));
		map.put("commentType", 2);
		resultMap.put("commentTotal", courseCommentService.queryTotal(map));
		//查询播放次数
		WfChapterRecordEntity chapterRecord = chapterRecordService.queryObject(chapterId);
		resultMap.put("playTotal", null != chapterRecord ? chapterRecord.getNumber() : 0);
		resultMap.put("great", courseCommentService.queryGreat(greatMap).size() > 0 ? 1 : 0);
		resultMap.put("collection", collectionService.queryCollectionList(collMap).size() > 0 ? 1 : 0);
		Distribution d1 = distributionService.queryDistributionByKey("h5_notice");
		Distribution d2 = distributionService.queryDistributionByKey("ios_notice");
		Distribution d3 = distributionService.queryDistributionByKey("android_notice");
		resultMap.put("h5_notice", d1 != null ? d1.getRemark() : "");
		resultMap.put("ios_notice", d2 != null ? d2.getRemark() : "");
		resultMap.put("android_notice", d3 != null ? d3.getRemark() : "");
		Record record= recordService.selectByUseridAndChapterid(new Record(userId,chapterId,null,null,null));
		resultMap.put("videoTime",record != null ? (record.getTime()==null?"0":record.getTime()): "0");
		resultMap.put("distributionStatus", distributionService.searchGoods(courseChapter.getCourseid(), 1) ? 1 : 2);
		return R.ok(resultMap);
	}

	/**
	 * （new）课程包信息接口
	 */
	@RequestMapping("/vipCard")
	@ResponseBody
	@IgnoreToken
	public R newVipCard(HttpServletRequest req, Integer vipCardId) {
		Integer userId = ISsoLoginHelper.confirm(req);
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> collMap = new HashMap<>();
		Map<String, Object> resultMap = new HashMap<>();

		resultMap.put("access", -1);
		if(null != userId) {
			if (goodsOrderRelationService.queryOrderCode(
					new WfGoodsOrderRelationEntity(userId, vipCardId, 3)).size() > 0) {
				resultMap.put("access", 1);
			}
		}
		map.put("vipCardId", vipCardId);
		//查询是否收藏
		collMap.put("foreignId", vipCardId);
		collMap.put("userId", userId);
		//1.课程 2.章节 3.vip卡 4.商品
		collMap.put("type", 3);
		WfVipCardEntity vipCardEntity = vipCardService.queryObject(vipCardId);
		List<WfCourseInfoEntity> courseInfoList = vipCardService.queryOnlyCourseList(map);
		vipCardEntity.setLabel(courseInfoService.queryCourseLabel(3, vipCardEntity.getId()));
		resultMap.put("courseInfoList", courseInfoList);
		resultMap.put("vipCardEntity", vipCardEntity);
		resultMap.put("vipteacherList", vipCardService.queryTeacherList(map));
		resultMap.put("collection", collectionService.queryCollectionList(collMap).size() > 0 ? 1 : 0);
		Distribution d1 = distributionService.queryDistributionByKey("h5_notice");
		Distribution d2 = distributionService.queryDistributionByKey("ios_notice");
		Distribution d3 = distributionService.queryDistributionByKey("android_notice");
		resultMap.put("h5_notice", d1 != null ? d1.getRemark() : "");
		resultMap.put("ios_notice", d2 != null ? d2.getRemark() : "");
		resultMap.put("android_notice", d3 != null ? d3.getRemark() : "");
		resultMap.put("distributionStatus", distributionService.searchGoods(vipCardId, 3) ? 1 : 2);
		return R.ok(resultMap);
	}

	/**
	 * 保存学习时间
	 */
	@RequestMapping("/saveStudyTime")
	@ResponseBody
	public R saveStudyTime(HttpServletRequest req, Integer chapterId, Integer videoTime, Integer studyTime) {
		Integer userId = ISsoLoginHelper.confirm(req);

		long nowTime =System.currentTimeMillis();
		long todayStartTime = nowTime - (nowTime + TimeZone.getDefault().getRawOffset())% (1000*3600*24);
		Record record= recordService.selectByCreateTime(new Record(userId,chapterId,null,null,todayStartTime));
        System.out.println("chapterId:"+chapterId);
        System.out.println("videoTime:"+videoTime);
        System.out.println("studyTime:"+studyTime);
        Integer total =  recordService.queryTotal(userId);
        if(total>130){//每当条数超过130条，删除第101-131条
            Map<String,Object> map = new HashMap();
            map.put("userId",userId);
            map.put("num",total-110);
            recordService.delRecord(map);
        }
		if(record == null){
            System.out.println("record:进入空");
			Integer in= recordService.insert(new Record(userId,chapterId,videoTime,null==studyTime?0:studyTime,new Date().getTime()));
		}else{
            System.out.println("record:"+record.toString());
			record.setTime(null==videoTime?0:videoTime);
			record.setStudytime(record.getStudytime()+(null==studyTime?0:studyTime));
			record.setCreatetime(new Date().getTime());
			recordService.update(record);
		}
		if((null==studyTime?0:studyTime)>0){
            Map<String,Object> map1 = new HashMap();
            map1.put("userId",userId);
            map1.put("num",studyTime);
            System.out.println("****************************studyTime："+studyTime);
            memberService.addStudyTime(map1);
        }

		return R.ok();
	}

	/**
	 * 学习时长
	 */
	@RequestMapping("/getStudyTime")
	@ResponseBody
	public R getStudyTime(HttpServletRequest req) {
		Integer userId = ISsoLoginHelper.confirm(req);
		long nowTime =System.currentTimeMillis();
		long todayStartTime = nowTime - (nowTime + TimeZone.getDefault().getRawOffset())% (1000*3600*24);//当天凌晨时间戳
		Integer todayTime= recordService.selectStudyTime(new Record(userId,null,null,null,todayStartTime));//当天
		/*Integer totalTime= recordService.selectStudyTime(new Record(userId,null,null,null,0L));*///所有
        Integer totalTime =  memberService.queryObject(userId).getStudyTime();
		R r = R.ok();
		int minutes = 0;
		double hour = 0;
		if(todayTime!=null){
			minutes = todayTime / 60;
		}
		if(totalTime!=null){
			hour = Math.round(totalTime * 10 / 3600)/10.0;
		}
		r.put("todayTime",minutes);
		r.put("totalTime",hour);
		return r;
	}

	/**
	 * 初始化member学习时长
	 */
	@RequestMapping("/setStudyTime")
	@ResponseBody
	@IgnoreSign
	@IgnoreToken
	public R setStudyTime(HttpServletRequest req) {
		List<WfMemberEntity> list = memberService.queryList(null);
		for (int i = 0; i < list.size(); i++) {
			memberService.updateStudyTime(list.get(i));
		}
		return R.ok();
	}

	/**
	 * 学习报告
	 */
	@RequestMapping("/getStudyReport")
	@ResponseBody
	public R getStudyReport(HttpServletRequest req) {
		R r = R.ok();
		Integer userId = ISsoLoginHelper.confirm(req);
		int warn = 0;       //提醒今天学习
		double hour = 0;	//总学习时长
		int studyDays = 0;	//连续学习天数
		int flag = 0;       //是否继续统计学习天数(0 继续，1 否)
		int count = 0;		//记录7天的标识
		String rili = "";	//7天的日期
		Integer studyTime=0;//每天学习秒数
		String dateArr[] = new String[7];
		Integer tArr[] = new Integer[7];
		long nowTime =System.currentTimeMillis();//今天时间戳
		long todayStartTime = nowTime - (nowTime + TimeZone.getDefault().getRawOffset())% (1000*3600*24);//今天凌晨

		while(count<7 || flag==0){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("userId",userId);
			if(count == 0){
				map.put("startTime", todayStartTime);
				map.put("endTime", nowTime);
			}else if(count<7){
				map.put("startTime", todayStartTime - count * 1000 * 3600 * 24);
				map.put("endTime", todayStartTime - (count - 1) * 1000 * 3600 * 24);
			}else if(count>=7){
				map.put("startTime", todayStartTime - studyDays * 1000 * 3600 * 24);
				map.put("endTime", todayStartTime - (studyDays - 1) * 1000 * 3600 * 24);
			}
			studyTime= recordService.selectTimeByTime(map);
			if(count<7) {//统计7天
				rili = new SimpleDateFormat("MM-dd").format(new Date(todayStartTime - count * 1000 * 3600 * 24));
				dateArr[count] = rili;//记录今天日期
				tArr[count] = (null==studyTime||studyTime==0) ? 0 : studyTime/60;//学习时长
				count = count + 1;
			}
			if(null!=studyTime && (studyTime/60)!=0 && flag==0){//连续学习天数
				studyDays = studyDays + 1;
			}else{
				if(count != 1){
					flag = 1;
				}else{
					warn = 1;
				}
			}
		}
		//总共学习时长
		Integer totalTime= recordService.selectStudyTime(new Record(userId,null,null,null,0L));//所有
		if(null!=totalTime && totalTime!=0){
			hour = Math.round(totalTime * 10 / 3600)/10.0;
		}

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userid",userId);
		map.put("type",1);
		Integer mp3 =  recordService.selectCount(map);
		map.put("type",2);
		Integer mp4 =  recordService.selectCount(map);

		r.put("studyDays",studyDays);//连续学习天数
		r.put("dateArr", dateArr);//7天日期
		r.put("tArr", tArr);//7天学习分钟
		r.put("totalTime",hour);//学习时间
		r.put("mp3",mp3);//音频
		r.put("mp4",mp4);//视频
		r.put("warn",warn);//学习提示
		r.put("work",courseWorkService.selectCount(userId));//视频
		return r;
	}

	/**
	 * 验证评论管理员身份
	 */
	@RequestMapping("/commentManager")
	@ResponseBody
	public R commentManager(HttpServletRequest req) {
		Integer uid = ISsoLoginHelper.confirm(req);
		WfCommenmanagerEntity commenmanager = commenmanagerService.queryObject(uid);
		if(commenmanager == null){
			return R.ok().put("flag", 0);
		}else{
			return R.ok().put("flag", 1);
		}
	}

	/**
	 * 评论分页管理接口
	 */
	@RequestMapping("/manageComment")
	@ResponseBody
	@IgnoreToken
	public R manageComment(HttpServletRequest req) {

		int page = Integer.valueOf(req.getParameter("page"));

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("page", page);
		//查询列表数据
		Query query = new Query(map);
		//评论总数
		map.put("commentType", 2);
		int commentTotal = courseCommentService.queryTotal(map);
		//评论列表
		List<WfCourseCommentEntity> courseCommentList = courseCommentService.queryManageCommentList(query);
		PageUtils pageUtil = new PageUtils(courseCommentList, commentTotal, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 用户缓存记录
	 */
	@RequestMapping("/saveDownload")
	@ResponseBody
	public R saveDownload(HttpServletRequest req) {
		Integer uid = ISsoLoginHelper.confirm(req);//获取userid
		//获取章节id
		String chapterId = req.getParameter("chapterId");
		//获取来源（andorid/ios）
		String type = req.getParameter("type");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("uid",uid);
		map.put("chapterid",chapterId);
		List<WfCourseDownloadLogsEntity> courseDownloadLogsList = ourseDownloadLogsService.queryList(map);
		if(courseDownloadLogsList.size()==0) {
			WfCourseDownloadLogsEntity log = new WfCourseDownloadLogsEntity();
			log.setUserid(uid);
			log.setType(type);
			log.setDltime(new Date());
			log.setChapterid(Integer.valueOf(chapterId));
			ourseDownloadLogsService.save(log);
		}
		return R.ok();
	}

	@RequestMapping("recommend")
	@ResponseBody
	@IgnoreToken
	public R recommend() {
		R r = R.ok();
		try {
			r.put("result", courseInfoService.recommend());
		} catch (Exception e) {
			r = R.error(500, "服务器异常！");
			log.error(e.getMessage(), e);
		}
		return r;
	}
}
